﻿#include "MetricesPainter.h"

MetricesPainter::MetricesPainter(QObject *parent, QCustomPlot *plot, const std::deque<MData::KLineItem> &kItems)
	: QObject(parent), m_plot(plot), m_kItems(kItems)
{
}

MetricesPainter::~MetricesPainter()
{
}

void MetricesPainter::DrawMetrices(const std::map<std::string, std::shared_ptr<MetData::MetriceBase>> &pMetrics)
{
	for (std::map<std::string, std::shared_ptr<MetData::MetriceBase>>::const_iterator it = pMetrics.begin(); it != pMetrics.end(); ++it)
	{
		switch (it->second->GetType())
		{
		case MetData::Metrice_MA:
			drawMA(it->second);
			break;
		case MetData::Metrice_MACD:
			drawMACD(it->second);
			break;
		case MetData::Metrice_ATR:
			break;
		case MetData::Metrice_Boll:
			break;
		case MetData::Metrice_OVB:
			break;
		case MetData::Metrice_Wave:
			drawWave(it->second);
			break;
		case MetData::Metrice_DividingLine:
			drawGoldLine(it->second);
			break;
		}
	}
}

void MetricesPainter::drawMA(std::shared_ptr<MetData::MetriceBase> pMetrice)
{
	std::shared_ptr<MetData::MAMetrice> pMAMetrice = std::dynamic_pointer_cast<MetData::MAMetrice>(pMetrice);
	if (pMAMetrice.get() == nullptr)
	{
		return;
	}
	QVector<double> timeDatas, MADatas;
	for (int ki = 0; ki < pMAMetrice->Line.size(); ++ki)
	{
		timeDatas.append(pMAMetrice->StartIndex + ki);
		MADatas.append(pMAMetrice->Line.at(ki));
	}
	QColor pCol(255, 255, 0, 255);
	QPen tPen(pCol);
	QCPGraph *graph = m_plot->addGraph();
	graph->setName(QString::fromLocal8Bit(pMAMetrice->Name.c_str()));
	graph->setData(timeDatas, MADatas);
	graph->setPen(tPen);
	graph->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, QPen(pCol, 2), QBrush(Qt::white), 8));
}

void MetricesPainter::drawMACD(std::shared_ptr<MetData::MetriceBase> pMetrice)
{
	std::shared_ptr<MetData::MACDMeterice> ptMetrice = std::dynamic_pointer_cast<MetData::MACDMeterice>(pMetrice);
	if (ptMetrice.get() == nullptr)
	{
		return;
	}

}

void MetricesPainter::drawWave(std::shared_ptr<MetData::MetriceBase> pMetrice)
{
	std::shared_ptr<MetData::Wave> ptMetrice = std::dynamic_pointer_cast<MetData::Wave>(pMetrice);
	if (ptMetrice.get() == nullptr)
	{
		return;
	}
	const std::deque<std::pair<int, float>> &pDatas = ptMetrice->Values;
	QVector<double> timeDatas, pvDatas;
	for (std::deque<std::pair<int, float>>::const_iterator itData = pDatas.begin(); itData != pDatas.end(); ++itData)
	{
		timeDatas.append(itData->first);
		pvDatas.append(itData->second);
	}
	QCPGraph *graph = m_plot->addGraph();
	graph->setName(QString::fromLocal8Bit(ptMetrice->Name.c_str()));
	graph->setData(timeDatas, pvDatas);
	QColor pCol(255, 215, 0, 255);
	QPen tPen(pCol);
	graph->setPen(tPen);
	graph->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, QPen(pCol, 2), QBrush(Qt::white), 8));
}

void MetricesPainter::drawGoldLine(std::shared_ptr<MetData::MetriceBase> pMetrice)
{
	std::shared_ptr<MetData::DividineLine> ptMetrice = std::dynamic_pointer_cast<MetData::DividineLine>(pMetrice);
	if (ptMetrice.get() == nullptr)
	{
		return;
	}
	QVector<double> timeDatas, pvDatas;
	timeDatas.append(0);
	timeDatas.append(m_kItems.size() - 1);
	pvDatas.append(ptMetrice->Value);
	pvDatas.append(ptMetrice->Value);
	QCPGraph *graph = m_plot->addGraph();
	graph->setName(QString::fromLocal8Bit(ptMetrice->Name.c_str()));
	graph->setData(timeDatas, pvDatas);
	graph->setLineStyle(QCPGraph::lsLine);
	QColor pCol(218, 165, 32, 255);
	QPen tPen(pCol);
	graph->setPen(tPen);
	graph->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, QPen(pCol, 2), QBrush(Qt::white), 8));
}
